

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class SungJukV2Client extends JFrame {

	private JPanel contentPane;
	private JTextField nameTxt;
	private JTextField korTxt;
	private JTextField engTxt;
	private JTextField mathTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SungJukV2Client frame = new SungJukV2Client();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SungJukV2Client() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel sungJuk = new JLabel(":: 성적처리 프로그램 ::");
		
		JLabel name = new JLabel("이    름");
		
		nameTxt = new JTextField();
		nameTxt.setColumns(10);
		
		JLabel kor = new JLabel("국    어");
		
		korTxt = new JTextField();
		korTxt.setColumns(10);
		
		JLabel eng = new JLabel("영    어");
		
		engTxt = new JTextField();
		engTxt.setColumns(10);
		
		JLabel math = new JLabel("수    학");
		
		mathTxt = new JTextField();
		mathTxt.setColumns(10);
		
		JButton saveBtn = new JButton("입 력 완 료");
		saveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//입력받은 성적 데이터를 SungJukV2VO에 저장 
				SungJukV2VO sj = new SungJukV2VO(nameTxt.getText(), Integer.parseInt(korTxt.getText()) ,Integer.parseInt(engTxt.getText()),Integer.parseInt(mathTxt.getText()));
				
				//SungJukV2Server에 접속하고 
				//SungJunkV2VO 객체를 서버에 전송
				try{
					System.out.println("[클라이언트] 서버에 접속을 시도합니다...");
					Socket connection = new Socket("124.124.124.223", 956);
					
					ObjectInputStream ois = null;
					ObjectOutputStream oos = null;
					
					//서버로 성적 객체를 전송함 
					oos = new ObjectOutputStream(connection.getOutputStream());
					oos.writeObject(sj);
					
					//서버에서 성적객체를 받아옴
					ois = new ObjectInputStream(connection.getInputStream());
					sj = (SungJukV2VO) ois.readObject();
					
				}catch(Exception ex){
					ex.printStackTrace();
				}
				JOptionPane.showMessageDialog(null, sj.toString());
			
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(143, Short.MAX_VALUE)
					.addComponent(sungJuk)
					.addGap(174))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(92)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(math, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
							.addGap(61)
							.addComponent(mathTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
							.addGroup(gl_contentPane.createSequentialGroup()
								.addComponent(name)
								.addGap(68)
								.addComponent(nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGroup(gl_contentPane.createSequentialGroup()
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
									.addComponent(kor, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
									.addComponent(eng, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
								.addGap(54)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
									.addComponent(korTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(engTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))))
					.addGap(107))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(163)
					.addComponent(saveBtn)
					.addContainerGap(181, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(sungJuk)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(name)
						.addComponent(nameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(korTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(kor))
					.addGap(27)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(engTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(eng))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(36)
							.addComponent(math))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(32)
							.addComponent(mathTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
					.addComponent(saveBtn)
					.addGap(4))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
